package com.dal.common.utils;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.*;
import java.net.URL;
import java.net.URLConnection;

public class ImageCrawler {

    public static void main(String[] args) {
        String url = "https://www.baidu.com/baidu?ie=utf-8&wd=%E5%9B%BE%E7%89%87"; // 替换成要爬取的网站URL
        String saveDir = "/Users/jian/Desktop/IO/images"; // 图片保存目录

        try {
            // 发起HTTP请求获取网页内容
            Document doc = Jsoup.connect(url).get();

            // 创建保存图片的目录
            new File(saveDir).mkdirs();

            // 在网页中查找所有的<img>标签
            Elements imgTags = doc.select("img");

            for (Element imgTag : imgTags) {
                // 提取图片的URL
                String imgUrl = imgTag.attr("src");

                // 发起HTTP请求获取图片内容
                URL imageUrl = new URL(imgUrl);
                URLConnection connection = imageUrl.openConnection();
                InputStream inputStream = connection.getInputStream();

                // 从URL中提取图片名称
                String imgName = saveDir + "/" + imgUrl.substring(imgUrl.lastIndexOf("/") + 1);

                // 保存图片到本地
                try (BufferedInputStream in = new BufferedInputStream(inputStream);
                     FileOutputStream fileOutputStream = new FileOutputStream(imgName)) {
                    byte[] dataBuffer = new byte[1024];
                    int bytesRead;
                    while ((bytesRead = in.read(dataBuffer, 0, 1024)) != -1) {
                        fileOutputStream.write(dataBuffer, 0, bytesRead);
                    }
                    System.out.println("Saved image: " + imgName);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}